//
//  UrlRequestManager.m
//  LO_MUSIC
//
//  Created by lanou3g on 16/3/2.
//  Copyright © 2016年 徐强. All rights reserved.
//

#import "UrlRequestManager.h"

@interface UrlRequestManager()
@property (nonatomic,strong)NSArray *arr;

@end

@implementation UrlRequestManager

+ (void)requestWith:(requestUrlType)type URL:(NSString *)URL condition:(NSDictionary *)condition SuccessBlock:(successBlock)successBlock failBlock:(failBlock)failBlock{
    
    UrlRequestManager *manger = [[UrlRequestManager alloc]init];
    if (type == GET) {
        [manger get:URL condition:condition success:successBlock fail:failBlock];
    }else{
        [manger post:URL condition:condition success:successBlock fail:failBlock];
    }
    
    
}
/**
 *   GET
 *  @param url url
 *  @param dic 参数
 *  @param success   成功的block
 *  @param faile     失败的block
 */
- (void)get:(NSString *)url condition:(NSDictionary *)condition success:(successBlock)success fail:(failBlock)fail{
    NSURLSession *session = [NSURLSession sharedSession];
    //拼接url
    __block NSMutableString *urlStr = [NSMutableString stringWithString:url];
    //拼接？
    [urlStr appendString:@"?"];
    //condition:快速枚举，为网址后半部分添加完整
    [condition enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        [urlStr appendString:[NSString stringWithFormat:@"%@=%@&",key,obj]];
    }];
    [urlStr substringToIndex:[urlStr length]-2];
    //转码
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:urlStr];
    NSString *urlString = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:set];
    //设置request
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    //设置请求方式
    [request setHTTPMethod:@"GET"];
    NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data && !error) {
            id item = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
            if (success) {
                success(item);
                return ;
            }else{
                NSLog(@"successBlock为空");
                return;
            }
        }
        if (fail) {
            fail(error);
        }
    }];
    [task resume];
}
/**
 *  Post
 *  @param url       url
 *  @param condition 参数
 *  @param success   成功的block
 *  @param faile     失败的block
 */
- (void)post:(NSString *)url condition:(NSDictionary *)condition success:(successBlock)success fail:(failBlock)fail{
    NSURLSession *session = [NSURLSession sharedSession];
    //拼接url
    NSMutableString *urlStr = [NSMutableString stringWithString:url];
    //拼接？
    [urlStr appendString:@"?"];
    //转码
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:urlStr];
    NSString *urlString = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:set];
    //body
    __block NSMutableString *bodyStr = [NSMutableString string];
    [condition enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        [bodyStr appendString:[NSString stringWithFormat:@"%@=%@&",key,obj]];
    }];
    [bodyStr substringToIndex:[bodyStr length]-2];
    //设置request
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    //
    [request setHTTPMethod:@"POST"];
    //设置body
    NSData *date = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:date];
    
    NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data && !error) {
            id item = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
            if (success) {
                success(item);
                return ;
            }else{
                NSLog(@"successBlock为空");
                return;
            }
        }
        if (fail) {
            fail(error);
        }
    }];
    [task resume];
    
}
single_implementation(UrlRequestManager)
@end
